import pandas as pd
import requests
import re
import json
from urllib.parse import urlsplit, parse_qsl
from bs4 import BeautifulSoup
import json

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'
}


# 获取评论
# https://club.jd.com/clubservice.aspx?method=GetCommentsCount&referenceIds=27354256546&callback=jQuery479643&_=1622089080746
# https://club.jd.com/comment/productPageComments.action?callback=jQuery8202412&productId=27354256546&score=0&sortType=5&page=0&pageSize=10&pin=jd_578d8fd43fd5d&_=1622082724496

def extract():
    # read_data = read_data[~ read_data['type'].str.contains('未知')]  # 删除某列包含特殊字符的行
    df = pd.read_excel('cos-tm1.xlsx')
    print(df.columns)
    df.drop_duplicates(subset=['映射网址'], keep='first', inplace=True)
    # df = df[df['映射网址'].str.contains('tmall')]
    df.to_excel('cos-tm2.xlsx')


def get_content(sid, mid):
    url = f'https://cd.jd.com/description/channel?skuId={sid}&mainSkuId={mid}&charset=utf-8&cdn=2'
    rep = requests.get(url, headers=headers)
    return rep.json()['content'].replace('data-lazyload', 'src')


def get_info(sid):
    url = f'https://npcitem.jd.hk/{sid}.html'
    rep = requests.get(url, headers=headers)
    pattern = re.compile(r"colorSize: \[(.*?)\], ", re.MULTILINE | re.DOTALL)
    cont = re.findall(pattern, rep.content.decode())[0]

    json_cont = json.loads('[' + cont + ']')
    print(json_cont)
    filter_cont = [x for x in json_cont if x['skuId'] == int(sid)][0]
    print(filter_cont)
    # filter_cont = [x for x in json_cont if x['skuId'] == int(sid)][0][list(json_cont[0].keys())[1]]
    return filter_cont


def get_name(sid):
    url = f'https://npcitem.jd.hk/{sid}.html'
    rep = requests.get(url, headers=headers)
    pattern = re.compile(r"name: '(.*?)'", re.MULTILINE | re.DOTALL)
    cont = re.findall(pattern, rep.content.decode())[0]
    return cont


def get_id():
    url = 'https://npcitem.jd.hk/27354256546.html'
    rep = requests.get(url, headers=headers)
    pattern = re.compile(r"desc: '(.*?)',", re.MULTILINE | re.DOTALL)
    cont = re.findall(pattern, rep.content.decode())[0]
    query = dict(parse_qsl(urlsplit(cont).query))
    skuId = query.get('skuId')
    mainSkuId = query.get('mainSkuId')
    content = get_content(skuId, mainSkuId)
    print(content)


def get_price():
    url = 'https://p.3.cn/prices/mgets?type=1&pdbp=0&skuIds=J_27354256546&ext=11100000&source=item-pc'
    rep = requests.get(url, headers=headers)
    op = rep.json().get('op')
    m = rep.json().get('m')
    return op, m


def get_images():
    url = 'https://npcitem.jd.hk/31497066274.html#crumb-wrap'
    rep = requests.get(url, headers=headers).content.decode()
    soup = BeautifulSoup(rep, 'lxml')
    imgs = ['https://img13.360buyimg.com/cms/' + x.get('data-url') for x in soup.select('#spec-list > ul > li > img')]
    print(imgs)
    return json.dumps(imgs)


if __name__ == '__main__':
    # get_content()
    # get_id()
    # extract()
    # get_images()
    # 1967052147
    # https://www.cosme-de.net/pd/products/CH00521?product_no=CH00521
    # get_info(72488038456)
    # get_name(1967052147)
    # extract()
    get_images()